package com.mxchip.livestar.base

import android.app.Application
import android.util.Log
import androidx.lifecycle.*
import com.mxchip.livestar.repository.CallLogRepository
import com.mxchip.livestar.ui.Call
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import com.mxchip.livestar.R
import com.mxchip.livestar.ui.GroupCall
import com.mxchip.livestar.ui.P2PSingleCall

/**
 * 通话记录 数据管理类
 */
class CallLogViewModel(application: Application) : AndroidViewModel(application) {

    private val callLogRepository: CallLogRepository = CallLogRepository.getInstance(application.applicationContext)


    val callLogAll: LiveData<List<RecentCallBean>> by lazy {
        Log.e("RecentCallsViewModel", "Thread.currentThread()" + Thread.currentThread().name)
        callLogRepository.all
    }
    private val mMyNumber = MutableLiveData<String>()

    val myNumber: LiveData<String>
        get() = mMyNumber

    fun setMyNumber(number: String) {
        mMyNumber.value = number
    }

    /**
     * 插入通话记录
     */
    fun createCallLog(number: String,type:String) {

        viewModelScope.launch(Dispatchers.Main) {
            // callLogRepository.insert(name, number)

            var name = callLogRepository.getContactNameByNumber(number)

            if (name == null) {
                name = ""
            }

            callLogRepository.insert(number, name, "60", type, "0")
        }
    }


    /**
     * 删除通话记录
     */
    fun deleteCallLogById(id: String) {
        callLogRepository.deleteById(id)
    }

    fun createCallLog(number: String, name: String?, duration: String, type: String, isNew: String) {
        callLogRepository.insert(number, name, duration, type, isNew)
    }

    /**
     * 创建通话类型
     */
    fun makeCall(number: String): Call {


        if (number.isNullOrBlank()) {
            throw Throwable(getString(R.string.input_number))
        }

        val call = when {
            number.length <= 8 -> {

                val aNumber = number.padStart(8, '0')

                if (aNumber == myNumber.value) {
                    throw Throwable(getString(R.string.called_local_number))
                }

                P2PSingleCall(aNumber)
            }

            number.length == 11 -> {

                if (number == myNumber.value) {
                    throw Throwable(getString(R.string.called_local_number))
                }

                P2PSingleCall(number)
            }

            number.length == 12 -> {
                if (number.contains("#")) {
                    P2PSingleCall(number)
                } else {
                    GroupCall(number)
                }
            }

            number.length == 15 -> {
                if (number.contains("#")) {
                    P2PSingleCall(number)
                } else {
                    throw Throwable(getString(R.string.called_number_error))
                }
            }

            number.length == 16 -> {
                if (number.contains("#")) {
                    GroupCall(number)
                } else {
                    throw Throwable(getString(R.string.called_number_error))
                }
            }
            else -> {
                throw Throwable(getString(R.string.called_number_error))
            }
        }


        return call
    }

}